//
//  hiddenView.m
//  GiftSay
//
//  Created by apple on 16/1/13.
//  Copyright © 2016年 liyinkai. All rights reserved.
//

#import "hiddenView.h"
#import "ShowArrayCollectionViewCell.h"
@interface hiddenView ()<UICollectionViewDataSource, UICollectionViewDelegate>

@end

@implementation hiddenView
-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:@"NightModeOpen" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:@"NightModeOff" object:nil];
        UIView *grayview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.width / 10.3)];
        grayview.backgroundColor = [UIColor grayColor];
        [self addSubview:grayview];
        UILabel *grayLabel = [[UILabel alloc] initWithFrame:grayview.bounds];
        grayLabel.text = @"点击添加更多频道";
        grayLabel.textColor = [UIColor whiteColor];
        grayLabel.font = [UIFont systemFontOfSize:14 * SCREEN_WIDTH];
        [grayview addSubview:grayLabel];
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        int width;
        if ((NSInteger)self.width % 4 == 0) {
            width = self.width;
        } else {
            width = self.width / 4 * 4;
        }
        layout.itemSize = CGSizeMake(width / 4, width / 4 / 2);
        
        self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, grayview.height, self.width, self.height - grayview.height) collectionViewLayout:layout];
        self.collection.delegate = self;
        self.collection.dataSource = self;
        self.collection.backgroundColor = [UIColor whiteColor];
        [self.collection registerClass:[ShowArrayCollectionViewCell class] forCellWithReuseIdentifier:@"ShowArrayCollectionViewCell"];
        [self addSubview:self.collection];
        
        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES) {
            self.backgroundColor = kNightModeColor;
            self.collection.backgroundColor = kNightModeColor;
        } else {
            self.backgroundColor = [UIColor whiteColor];
            self.collection.backgroundColor = [UIColor whiteColor];
        }
    }
    return self;
}
- (void)refresh {
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES) {
        self.backgroundColor = kNightModeColor;
        self.collection.backgroundColor = kNightModeColor;
    } else {
        self.backgroundColor = [UIColor whiteColor];
        self.collection.backgroundColor = [UIColor whiteColor];
    }
    [self.collection reloadData];
}
-(void)setDataArray:(NSMutableArray *)dataArray {
    _dataArray = dataArray;
    for (int i = 0; i < self.dataArray.count; i++) {
    }
    [self.collection reloadData];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ShowArrayCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ShowArrayCollectionViewCell" forIndexPath:indexPath];
    cell.name = self.dataArray[indexPath.item];
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    ShowArrayCollectionViewCell *cell = (ShowArrayCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    [self.delegate hiddendele:cell.name];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

@end
